﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBAccessLib;

namespace LineWatch_WinForm
{
    public partial class LoginView : Form
    {
        public LoginView()
        {
            InitializeComponent();

#if DEBUG
            this.txtUserName.Text = "Admin";
            this.txtPassword.Text = "Admin";
#endif
        }

        /// <summary>
        /// Sign In
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSignIn_Click(object sender, EventArgs e)
        {
            this.lblInfo.Text = string.Empty;
            this.Refresh();
            this.Update();

            bool canSetShift = false;
            bool loginSuccessfully = false;
            bool hasRight = false;

            string userName = this.txtUserName.Text.Trim();
            string password = this.txtPassword.Text.Trim();

            if (!string.IsNullOrWhiteSpace(userName)
                && !string.IsNullOrWhiteSpace(password))
            {
                loginSuccessfully = Account.SignedIn(userName, password, ref canSetShift, ref hasRight);
            }

            if (loginSuccessfully)
            {
                LoginInfo.Authorized = true;
                LoginInfo.UserName = userName;
                ConfigInfo.InitConfigInfo();
                ConfigInfo.InitAllPositions();

                this.Close();
            }
            else
            {
                LoginInfo.Authorized = false;
                this.lblInfo.Text = "用户名或密码不正确。";
            }
        }

        /// <summary>
        /// Quit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_Click(object sender, EventArgs e)
        {
            LoginInfo.Authorized = false;
            this.Close();
        }
    }
}
